package com.dudu.util;

import com.dudu.exception.ErrorCode;

import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serializable;

/**
 * Created by dudu on 16/8/5.
 */
@EqualsAndHashCode
@Data
public class WebResponse<T> implements Serializable {

    private static final long serialVersionUID = 218412192938273843L;
    private long code;
    private String msg;
    private T data;
    private PageDto pageDto;

    public WebResponse() {
        this.code = 0;
    }

    public WebResponse(T data) {
        this.code = 0;
        this.data = data;
    }

    public WebResponse(T data, PageDto pageDto) {
        this.code = 0;
        this.data = data;
        this.pageDto = pageDto;
    }

    /**
     * 请求错误，通过错误码定义错误
     *
     * @param errorCode
     */
    public WebResponse(ErrorCode errorCode) {
        this.code = errorCode.getCode();
        this.msg = errorCode.getMessage();
    }

    /**
     * 请求错误，通过错误码和自定义的错误信息来定义返回结果
     *
     * @param errorCode
     * @param msg
     */
    public WebResponse(ErrorCode errorCode, String msg) {
        this.code = errorCode.getCode();
        this.msg = msg;
    }

    public void setErrorCode(ErrorCode errorCode, String msg) {
        this.code = errorCode.getCode();
        this.msg = msg;
    }

    public void setErrorCode(ErrorCode errorCode) {
        this.code = errorCode.getCode();
        if (errorCode != null) {
            this.msg = errorCode.getMessage();
        }
    }
}
